package cn.zzszxyy.hr.service;

import cn.zzszxyy.hr.model.DepartmentEntity;
import cn.zzszxyy.hr.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-27
 * 说明:部门or科室
 */
@Service
public class DeptService {

    @Autowired
    private DepartmentRepository departmentRepository;

    public List<DepartmentEntity> findAll() {
        return departmentRepository.findAll();
    }

    public Page<DepartmentEntity> findAllWithPage(int pageIndex, int pageSize) {
        PageRequest pageRequest = new PageRequest(--pageIndex, pageSize);
        Page<DepartmentEntity> deptList = departmentRepository.findAll(pageRequest);
        return deptList;
    }

    public Page<DepartmentEntity> findByDeptNameLike(String deptName, int pageIndex, int pageSize) {
        PageRequest pageRequest = new PageRequest(--pageIndex, pageSize);
        return departmentRepository.findByDeptNameLike(deptName, pageRequest);
    }

    public DepartmentEntity findOne(int id) {
        return departmentRepository.findOne(id);
    }

    public DepartmentEntity save(DepartmentEntity departmentEntity) {
        return departmentRepository.save(departmentEntity);
    }

    @Transactional
    public void delete(int id) {
        //删除：本表记录
        departmentRepository.delete(id);
    }

}
